package com.geeguo.ebuilder.core.common.utils.key;

import com.geeguo.ebuilder.core.common.utils.worker.WorkerIdHelper;
import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.YitIdHelper;

public class SnowflakeIdGenerator {

    private static volatile boolean isInit = false;

    private static final Object lock = new Object();

    public static long nextId() {
        if (!isInit) {
            synchronized (lock) {
                if (!isInit) {
                    init();
                    isInit = true;
                }
            }
        }
        return YitIdHelper.nextId();
    }

    private static void init() {
        IdGeneratorOptions options = new IdGeneratorOptions(WorkerIdHelper.getWorkerId());
        options.WorkerIdBitLength = WorkerIdHelper.workerIdBits;
        options.SeqBitLength = 12;
        options.BaseTime = 1735660800000L;
        YitIdHelper.setIdGenerator(options);
    }
}